import { createWebHistory, createRouter } from 'vue-router'
import Layout from '@/layout'

// 公共路由
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index.vue')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login'),
    hidden: true
  },
  {
    path: '/sociallogin',
    component: () => import('@/views/socialLogin'),
    hidden: true
  },
  {
    path: '/register',
    component: () => import('@/views/register'),
    hidden: true
  },
  {
    path: '/:pathMatch(.*)*',
    component: () => import('@/views/error/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error/401'),
    hidden: true
  },
  {
    path: '/error',
    component: () => import('@/views/error/Error'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/pages/Dashboard/index.vue'),
        name: 'dashboard',
        meta: {
          title: "首页工作台",
          icon: "workdashboard"
        }
      }
    ]
  },
  {
    path: '/orders',
    name: 'OrderManage',
    component: Layout,
    meta: {
      title: '订单管理',
      icon: 'orders'
    }, children: [
      {
        path: 'list',
        name: 'OrderList',
        component: () => import('@/pages/OrderManage/index.vue'),
        meta: {
          title: '订单列表'
        },
      },
      {
        path: 'detail/:id',
        name: 'OrderDetail',
        component: () => import('@/pages/OrderManage/OrderDetail.vue'),
        meta: {
          title: '订单详情'
        }
      }
    ]
  },
  {
    path: '/inventory',
    name: 'InventoryManage',
    component: Layout,
    meta: {
      title: '库存管理',
      icon: "inventory"
    },
    children: [
      {
        path: 'material',
        name: 'InventoryMaterial',
        component: () => import('@/pages/InventoryManage/MaterialInventory.vue'),
        meta: {
          title: '物料库存'
        }
      },
      {
        path: 'product',
        name: 'InventoryProduct',
        component: () => import('@/pages/InventoryManage/ProductInventory.vue'),
        meta: {
          title: '成品库存'
        }
      }
    ]
  },
  {
    path: '/device-monitor',
    name: 'DeviceMonitor',
    component: Layout,
    meta: {
      title: '设备与工况监测',
      icon: "device-monitor"
    },
    redirect: '/device-monitor/health',
    children: [
      {
        path: 'health',
        name: 'DeviceHealth',
        component: () => import('@/pages/DeviceMonitor/DeviceHealth.vue'),
        meta: { title: '设备健康状况' }
      },
      {
        path: 'utilization',
        name: 'DeviceUtilization',
        component: () => import('@/pages/DeviceMonitor/DeviceUtilization.vue'),
        meta: { title: '机器利用率' }
      },
      {
        path: 'maintenance',
        name: 'DeviceMaintenance',
        component: () => import('@/pages/DeviceMonitor/DeviceMaintenance.vue'),
        meta: { title: '维护与检修' }
      }
    ]
  },
  {
    path: '/scheduling',
    name: 'Scheduling',
    component: Layout,
    meta: {
      title: '智能调度与优化',
      icon: "scheduling"
    },
    redirect: '/scheduling/global',
    children: [
      {
        path: 'global',
        name: 'GlobalScheduling',
        component: () => import('@/pages/Scheduling/GlobalScheduling.vue'),
        meta: { title: '全局调度' }
      },
      {
        path: 'reschedule',
        name: 'ReScheduling',
        component: () => import('@/pages/Scheduling/ReScheduling.vue'),
        meta: { title: '重调度' }
      },
      {
        path: 'joint',
        name: 'JointScheduling',
        component: () => import('@/pages/Scheduling/JointScheduling.vue'),
        meta: { title: '联合调度' }
      }
    ]
  },
  {
    path: '/process',
    name: 'ProcessManage',
    component: Layout,
    meta: {
      title: '工艺分段与工序管理',
      icon: "process"
    },
    children: [
      {
        path: 'line',
        name: 'ProductionLine',
        component: () => import('@/pages/ProcessManage/ProductionLine.vue'),
        meta: { title: '生产线与工序管理' }
      },
      {
        path: 'workorder',
        name: 'WorkOrderSplit',
        component: () => import('@/pages/ProcessManage/WorkOrderSplit.vue'),
        meta: { title: '工单拆分/合并' }
      }
    ]
  },
  {
    path: '/purchase',
    name: 'PurchaseManage',
    component: Layout,
    meta: {
      title: '采购与入库管理',
      icon: "purchase"
    },
    children: [
      {
        path: 'order',
        name: 'PurchaseOrder',
        component: () => import('@/pages/PurchaseManage/PurchaseOrder.vue'),
        meta: { title: '采购订单' }
      },
      {
        path: 'storage',
        name: 'StorageDetail',
        component: () => import('@/pages/PurchaseManage/StorageDetail.vue'),
        meta: { title: '入库明细' }
      }
    ]
  },
  {
    path: '/data-analysis',
    name: 'DataAnalysis',
    component: Layout,
    meta: {
      title: '数据分析与决策支持',
      icon: "dataAnalysis"
    },
    children: [
      {
        path: 'efficiency',
        name: 'EfficiencyAnalysis',
        component: () => import('@/pages/DataAnalysis/EfficiencyAnalysis.vue'),
        meta: { title: '生产效率分析' }
      },
      {
        path: 'process-stat',
        name: 'ProcessStatReport',
        component: () => import('@/pages/DataAnalysis/ProcessStatReport.vue'),
        meta: { title: '工序统计报表' }
      },
      {
        path: 'inventory-purchase',
        name: 'InventoryPurchaseAnalysis',
        component: () => import('@/pages/DataAnalysis/InventoryPurchaseAnalysis.vue'),
        meta: { title: '库存与采购分析' }
      }
    ]
  },
  {
    path: '/monitor-warn',
    name: 'MonitorWarn',
    component: Layout,
    meta: {
      title: '远程监控与预警中心',
      icon: "monitorWarn"
    },
    children: [
      {
        path: 'remote',
        name: 'RemoteMonitor',
        component: () => import('@/pages/MonitorWarn/RemoteMonitor.vue'),
        meta: { title: '虚拟远程监控' }
      },
      {
        path: 'alarm',
        name: 'AlarmPush',
        component: () => import('@/pages/MonitorWarn/AlarmPush.vue'),
        meta: { title: '生产告警推送' }
      },
      {
        path: 'log',
        name: 'EventLog',
        component: () => import('@/pages/MonitorWarn/EventLog.vue'),
        meta: { title: '事件日志' }
      }
    ]
  },
  {
    path: '/basic-data',
    name: 'BasicData',
    component: Layout,
    meta: {
      title: '基础资料设置',
      icon: "basicData"
    },
    redirect: '/basic-data/material',
    children: [
      {
        path: 'material',
        name: 'MaterialInfo',
        component: () => import('@/pages/BasicData/MaterialInfo.vue'),
        meta: { title: '物料信息维护' }
      },
      {
        path: 'device',
        name: 'DeviceArchive',
        component: () => import('@/pages/BasicData/DeviceArchive.vue'),
        meta: { title: '设备档案管理' }
      },
      {
        path: 'workshop',
        name: 'WorkshopConfig',
        component: () => import('@/pages/BasicData/WorkshopConfig.vue'),
        meta: { title: '产线/车间/工段配置' }
      },
      {
        path: 'personnel',
        name: 'PersonnelInfo',
        component: () => import('@/pages/BasicData/PersonnelInfo.vue'),
        meta: { title: '人员信息' }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.VITE_APP_ROUTER_PREFIX),
  routes: constantRoutes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})

export default router
